/**
 * get
 * @param {string} url 请求地址
 * @param {Object} params 请求参数
 * @param {JSON} type 数据类型
 * @param {Object} headers 请求头
 */
function get(url,params,type,headers={}){
    const p = new Promise((resolve,reject)=>{
        const xhr = new XMLHttpRequest()
        let temp = []
        for(let key in params){
            temp.push(`${key}=${params[key]}`)
        }
        xhr.open('get',`${url}?${temp.join('&')}`)
        xhr.onreadystatechange = ()=>{
            if(xhr.readyState === 4){
                if(xhr.status === 200){
                    let res = xhr.responseText
                    if(type == 'json')  res = JSON.parse(res)
                    resolve(res)
                }else{
                    reject(xhr.status)
                }
            }
        }
        for(let key in headers){
            xhr.setRequestHeader(key,headers[key])
        }
        xhr.send(null)
    })
    return p    
}

/**
 * post
 * @param {string} url 请求地址
 * @param {Object} params 请求参数
 * @param {JSON} type 数据类型
 * @param {Object} headers 请求头
 * @returns 
 */
function post(url,params,type,headers={}){
    const p = new Promise((resolve,reject)=>{
        const xhr = new XMLHttpRequest()
        let temp = []
        for(let key in params){
            temp.push(`${key}=${params[key]}`)
        }
        xhr.open('post',url)
        xhr.onreadystatechange = ()=>{
            if(xhr.readyState === 4){
                if(xhr.status === 200){
                    let res = xhr.responseText
                    if(type == 'json')  JSON.parse(res)
                    resolve(res)
                }else{
                    reject(xhr.status)
                }
            }
        }
        xhr.setRequestHeader('content-type','application/x-www-form-urlencoded')
        for(let key in headers){
            xhr.setRequestHeader(key,headers[key])
        }

        xhr.send(temp.join('&'))
    })
    return p    
}